﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Entropy.Ext.Test
{
	[TestClass]
	public class AsgBuilderTest
	{
		[TestMethod]
		public void ThatConstructorCalledInAnotherIsPromoted()
		{
			Type type = typeof (IPanelPanel);
			var c1 = new AstConstructor(type)
			{
				Args = new AstConstructorParameter[0]
			};

			var c2 = new AstConstructor(type)
			{
				Args = new[]
				{
					new AstConstructorParameter
					{
						Name = "f",
						Value = c1,
						AsJson = false
					}
				}
			};

			IAsgBuilder subject = new AsgBuilder(new ExtAccent(), new SequentialTypeNaming());
			var result = subject.Compile(new[] { c1, c2 }).ToArray();
			Assert.AreEqual(2, result.Length);
		}
		
		[TestMethod]
		public void ThatConstructorCalledTwiceInAnotherIsPromotedOnce()
		{
			Type type = typeof (IPanelPanel);
			var c1 = new AstConstructor(type)
			{
				Args = new AstConstructorParameter[0]
			};

			var c2 = new AstConstructor(type)
			{
				Args = new[]
				{
					new AstConstructorParameter
					{
						Name = "f",
						Value = c1,
						AsJson = false
					}
				}
			};

			var c3 = new AstConstructor(type)
			{
				Args = new[]
				{
					new AstConstructorParameter
					{
						Name = "f",
						Value = c1,
						AsJson = false
					}
				}
			};

			IAsgBuilder subject = new AsgBuilder(new ExtAccent(), new SequentialTypeNaming());
			var result = subject.Compile(new[] { c1, c2 , c3}).ToArray();
			Assert.AreEqual(3, result.Length);
		}

		[TestMethod]
		public void ThatConstructorCalledTwiceRecorsivelyIsPromotedOnce()
		{
			Type type = typeof (IPanelPanel);
			var c1 = new AstConstructor(type)
			{
				Args = new AstConstructorParameter[0]
			};

			var c2 = new AstConstructor(type)
			{
				Args = new[]
				{
					new AstConstructorParameter
					{
						Name = "f",
						Value = c1,
						AsJson = false
					}
				}
			};

			var c3 = new AstConstructor(type)
			{
				Args = new[]
				{
					new AstConstructorParameter
					{
						Name = "f",
						Value = c2,
						AsJson = false
					}
				}
			};

			IAsgBuilder subject = new AsgBuilder(new ExtAccent(), new SequentialTypeNaming());
			var result = subject.Compile(new[] { c1, c2, c3 }).ToArray();
			Assert.AreEqual(3, result.Length);
		}
	}
}
